package com.yhn.init.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.yhn.init.common.ErrorCode;
import com.yhn.init.constant.FileConstant;
import com.yhn.init.exception.ThrowUtils;
import com.yhn.init.service.FileService;
import com.yhn.init.utils.MinioUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 文件上传实现类
 *
 * @author yhnyhn123
 * @version 1.0
 * @description
 * @date 2024/3/8 10:27
 */
@Service
public class FileServiceImpl implements FileService {

    @Resource
    private MinioUtil minioUtil;

    /**
     * 上传图片
     *
     * @param file 上传的文件路径
     * @return 路径
     */
    @Override
    public String uploadImage(MultipartFile file, String path) {
        fileCheck(file);
        //利用FiLeUtils获取文件的后缀名
        String originalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(originalFilename);
        //若后缀名不在定义的常量合法的后缀名中则抛出异常
        ThrowUtils.throwIf(!FileConstant.VALID_IMG_SUFFIX.contains(suffix), ErrorCode.PARAMS_ERROR, "文件格式不合法");

        //生成新的文件名
        path = generatePath(path);
        minioUtil.uploadImage(path, file);
        return path;
    }


    @Override
    public String uploadExcel(MultipartFile file, String path) {
        fileCheck(file);
        String originalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(originalFilename);
        //若后缀名不在定义的常量合法的后缀名中则抛出异常
        ThrowUtils.throwIf(!FileConstant.VALID_FILE_SUFFIX.contains(suffix), ErrorCode.PARAMS_ERROR, "文件格式不合法");

        path = generatePath(path);
        minioUtil.uploadExcel(path, file);
        return path;
    }

    @Override
    public String uploadWord(MultipartFile file, String path) {
        fileCheck(file);
        String originalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(originalFilename);
        //若后缀名不在定义的常量合法的后缀名中则抛出异常
        ThrowUtils.throwIf(!FileConstant.VALID_WORD_SUFFIX.contains(suffix), ErrorCode.PARAMS_ERROR, "文件格式不合法");
        path = generatePath(path);
        minioUtil.uploadWordDocument(path, file);
        return path;
    }

    @Override
    public String uploadPdf(MultipartFile file, String path) {
        fileCheck(file);
        String originalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(originalFilename);
        //若后缀名不在定义的常量合法的后缀名中则抛出异常
        ThrowUtils.throwIf(!FileConstant.VALID_PDF_SUFFIX.contains(suffix), ErrorCode.PARAMS_ERROR, "文件格式不合法");
        path = generatePath(path);
        minioUtil.uploadPdf(path, file);
        return path;
    }

    @NotNull
    private static String generatePath(String path) {
        String filename = IdUtil.simpleUUID() + ".jpg";
        path += filename;
        return path;
    }

    private void fileCheck(MultipartFile multipartFile) {
        //获得文件的大小和原始文件名称
        long size = multipartFile.getSize();
        //若文件过大
        ThrowUtils.throwIf(size > FileConstant.FILE_MAX_SIZE, ErrorCode.PARAMS_ERROR, "文件大小超过最大限制");

    }
}
